#include "sessionfriendarea.h"
#include <QScrollBar>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QPainter>
#include "debug.h"
#include <QStyleOption>
#include "./model/data.h"

SessionFriendArea::SessionFriendArea(QWidget* parent)
	:QScrollArea(parent)
{
	//设置必要的属性,开启这个属性才能滚动
	this->setWidgetResizable(true);
	this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:2px;background-color:rgb(46,46,46);}");
	this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height:0px;}");
	this->setStyleSheet("QScrollArea { border: none; }");

	//把widget创建出来
	container = new QWidget();
	container->setFixedWidth(310);
	this->setWidget(container);

	QVBoxLayout* layout = new QVBoxLayout();
	layout->setContentsMargins(0, 0, 0, 0);
	layout->setSpacing(0);
	layout->setAlignment(Qt::AlignTop);

	container->setLayout(layout);

#if TEST_UI
	QIcon icon(":/resource/image/defaultAvatar.png");
	for (int i = 0; i < 30; i++)
	{
		this->addItem(ItemType::ApplyItemType,QString::number(1), icon, "张三" + QString::number(i), "最后一条消息" + QString::number(i));
	}
#endif 
}

SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text)
	:owner(owner), selected(false), messageLabel(nullptr)
{
	this->setFixedHeight(70);
	this->setStyleSheet("QWidget{ background-color:rgb(231,231,231);}");
	//创建一个网格布局管理器
	QGridLayout* layout = new QGridLayout();
	layout->setHorizontalSpacing(10);
	layout->setVerticalSpacing(0);
	layout->setContentsMargins(10, 0, 0, 0);
	this->setLayout(layout);
	//创建头像
	QPushButton* avatarBtn = new QPushButton();
	avatarBtn->setFixedSize(50, 50);
	avatarBtn->setIconSize(QSize(50, 50));
	avatarBtn->setIcon(avatar);
	avatarBtn->setStyleSheet("QPushButton {border: none;}");
	avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	//创建名字
	QLabel* nameLabel = new QLabel();
	nameLabel->setText(name);
	nameLabel->setStyleSheet("QLabel{font-size:18px;font-weight:600}");
	nameLabel->setFixedHeight(35);
	nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	//创建消息预览的label
	this->messageLabel = new QLabel();
	this->messageLabel->setText(text);
	this->messageLabel->setFixedHeight(35);
	this->messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

	layout->addWidget(avatarBtn, 0, 0, 2, 2);
	layout->addWidget(nameLabel, 0, 2, 1, 8);
	layout->addWidget(this->messageLabel, 1, 2, 1, 8);
}

void SessionFriendArea::clear()
{
	QLayout* layout = container->layout();
	for (int i = layout->count() - 1; i >= 0; i--)
	{
		QLayoutItem* item = layout->takeAt(i);
		if (item->widget())
		{
			delete item->widget();
		}
	}
}

//三种item其中的一个
void SessionFriendArea::addItem(ItemType itemType, const QString& id,const QIcon& avatar, const QString& name, const QString& text)
{
	SessionFriendItem* item = nullptr;
	if (itemType == ItemType::SessionItemType)
	{
		item = new SessionItem(this,id, avatar, name, text);
	}
	else if (itemType == ItemType::FriendItemType)
	{
		item = new FriendItem(this, id, avatar, name, text);
	}
	else if (itemType == ItemType::ApplyItemType)
	{
		item = new ApplyItem(this, id, avatar, name);
	}
	else
	{
		LOG() << "错误的item type"<< itemType;
		return;
	}
	
	container->layout()->addWidget(item);
}

void SessionFriendArea::clickItem(int index)
{
	if (index < 0 || index >= container->layout()->count()) {
		LOG() << "点击元素的下标超出范围" << index;
		return;
	}
	QLayoutItem* layoutItem =  container->layout()->itemAt(index);
	if (layoutItem == nullptr || layoutItem->widget() == nullptr) {
		LOG() << "指定的元素不存在" << index;
		return;
	}
	SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(layoutItem->widget());

}

void SessionFriendItem::paintEvent(QPaintEvent* event)
{
	QStyleOption opt;
	opt.initFrom(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SessionFriendItem::mousePressEvent(QMouseEvent* event)
{
	(void) event;
	select();
}

void SessionFriendItem::enterEvent(QEnterEvent* event)
{
	(void)event;
	if (this->selected) {
		return;
	}
	this->setStyleSheet("QWidget{ background-color:rgb(215,215,215);}");
}

void SessionFriendItem::leaveEvent(QEvent* event)
{
	(void)event;
	if (this->selected) {
		return;
	}
	this->setStyleSheet("QWidget{ background-color:rgb(231,231,231);}");
}

void SessionFriendItem::select()
{
	const QObjectList children = this->parentWidget()->children();
	for (auto it : children)
	{
		if (!it->isWidgetType()) {
			continue;
		}
		else {
			SessionFriendItem* item = dynamic_cast<SessionFriendItem*>(it);
			if (item->selected) {
				item->selected = false;
				item->setStyleSheet("QWidget{ background-color:rgb(231,231,231);}");
			}
		}
	}
	this->selected = true;
	this->setStyleSheet("QWidget{ background-color:rgb(210,210,210);}");
	this->active();
}

void SessionFriendItem::active()
{
	
}


SessionItem::SessionItem(QWidget* owner, const QString& chatSessionId, const QIcon& avatar, const QString& name, const QString& lastMessage)
	:SessionFriendItem(owner,avatar,name, lastMessage)
	, chatSessionId(chatSessionId)
{

}

FriendItem::FriendItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name, const QString& description)
	:SessionFriendItem(owner, avatar, name, description)
	, userId(userId)
{

}

ApplyItem::ApplyItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name)
	: SessionFriendItem(owner, avatar, name,"")
	, userId(userId)
{
	//移除父类的messagLabel
	QGridLayout* layout = dynamic_cast<QGridLayout*>(this->layout());
	layout->removeWidget(this->messageLabel);
	delete this->messageLabel;
	//创建两个按钮出来
	QPushButton* acceptBtn = new QPushButton();
	acceptBtn->setStyleSheet("QPushButton{border:0px};");
	acceptBtn->setText("同意");
	QPushButton* rejectBtn = new QPushButton();
	rejectBtn->setStyleSheet("QPushButton{border:0px};");
	rejectBtn->setText("拒绝");
	//添加到布局管理器中
	layout->addWidget(acceptBtn, 1, 2, 1, 1);
	layout->addWidget(rejectBtn, 1, 3, 1, 1);
}

void SessionItem::active()
{
	//todo，加载会话历史消息列表
	LOG() << "点击sessionItem触发的逻辑";
}

void FriendItem::active()
{
	//todo，激活对应的会话列表元素
	LOG() << "点击FriendItem触发的逻辑";
}

void ApplyItem::active()
{
	//todo，这个不需要实现任何内容
	LOG() << "点击ApplyItem触发的逻辑";
}
